<?php include _include(APP_PATH.'view/htm/header_thread.inc.htm');?>

<!--{hook thread_start.htm}-->

<div class="DiscussionPage">
    <!-- 显示作者所有帖子 -->
    <?php
        $_threadlist = thread__find_by_fid($thread['fid'],0,10,'tid');
        thread_list_access_filter($_threadlist, $gid);
    ?>
    <div class="DiscussionPage-list">
        <div class="DiscussionList">
            <ul class="DiscussionList-discussions">
                <?php foreach($_threadlist as $_thread) { ?>
                <li>
                    <div class="DiscussionListItem">
                        <div class="DiscussionListItem-content Slidable-content">
                            <a href="<?php echo url("user-$_thread[uid]");?>" class="DiscussionListItem-author" tabindex="-1">
                                <img class="Avatar" src="<?php echo $_thread['user_avatar_url'];?>">
                            </a>
                            <a href="<?php echo url("thread-$_thread[tid]");?>" class="DiscussionListItem-main">
                                <h3 class="DiscussionListItem-title"><?php echo $_thread['subject'];?></h3>
                                <ul class="DiscussionListItem-info">
                                    <li class="item-tags">
                                    <?php if(!$fid) { ?>
                                        <span class="TagsLabel tags-<?php echo $_thread['fid']; ?>">
                                            <span class="TagLabel colored">
                                                <span class="TagLabel-text"><?php echo $_thread['forumname'];?></span>
                                            </span><?php if(!empty($_thread['taglist'])) { foreach($_thread['taglist'] as $tag) { ?><span class="TagLabel colored tag-id"><?php echo $tag['name'];?></span>
                                            <?php }} ?>
                                        </span>
                                    <?php } ?>
                                    </li>
                                    <li class="item-terminalPost">
                                        <span>
                                        <?php if($_thread['lastuid']) { ?>
                                            <i class="icon fa fa-reply"></i>
                                            <span class="username" uid="<?php echo $_thread['lastuid'];?>"><?php echo $_thread['lastusername'];?></span> 回复于
                                            <span><?php echo $_thread['last_date_fmt'];?></span>
                                        <?php } else { ?>
                                            <span class="username" uid="<?php echo $_thread['uid'];?>"><?php echo $_thread['username'];?></span> 发表于
                                            <span><?php echo $_thread['create_date_fmt'];?></span>
                                        <?php } ?>
                                        </span>
                                    </li>
                                </ul>
                            </a>
                            <span class="DiscussionListItem-count" title=""><?php echo $_thread['posts'];?></span>
                        </div>
                    </div>
                </li>
                <?php } ?>
            </ul>
        </div>
    </div>
    <div class="DiscussionPage-discussion">
        <header class="Hero DiscussionHero DiscussionHero--colored text-white hero-<?php echo $forum['fid'];?>">
            <div class="container">
                <ul class="DiscussionHero-items">

                    <li class="item-badges">
                        <ul class="DiscussionHero-badges badges">
                            <?php if($thread['top'] > 0) { ?>
                            <li class="item-sticky">
                                <span class="Badge Badge--sticky" title="置顶" data-toggle="tooltip">
                                    <i class="icon fa fa-fw fa-thumb-tack Badge-icon"></i>
                                </span>
                            </li>
                            <?php } ?>
                            <?php if($thread['closed'] > 0) { ?>
                            <li class="item-locked">
                                <span class="Badge Badge--locked " title="锁定" data-toggle="tooltip">
                                    <i class="icon fa fa-fw fa-lock Badge-icon"></i>
                                </span>
                            </li>
                            <?php } ?>
                            <?php if($thread['digest'] > 0) { ?>
                            <li class="item-subscription">
                                <span class="Badge Badge--following " title="精华" data-original-title="精华">
                                    <i class="icon fa fa-fw fa-star Badge-icon"></i>
                                </span>
                            </li>
                            <?php } ?>
                        </ul>
                    </li>
                    <li class="item-tags">
                        <span class="TagsLabel tags-<?php echo $thread['fid']; ?>">
                            <a class="TagLabel colored" href="<?php echo url("forum-$fid");?>">
                                <span class="TagLabel-text"><?php echo $forum['name'];?></span>
                            </a>
                        </span>
                    </li>
                    <li class="item-title">
                        <h2 class="DiscussionHero-title"><?php echo $thread['subject'];?></h2>
                    </li>
                </ul>
            </div>
        </header>
        <div class="container">
            <nav class="DiscussionPage-nav">
                <ul>
                    <li class="item-controls">
                        <div class="btn-group Dropdown dropdown App-primaryControl Dropdown--split itemCount1">
                            <a class="SplitDropdown-btn btn btn-primary hasIcon" href="#newpost" role="btn" title="<?php echo lang('reply');?>">
                                <i class="icon fas fa-reply btn-icon"></i>
                                <span class="btn-label"><?php echo lang('reply');?></span>
                            </a>
                            <button class="Dropdown-toggle btn btn--icon btn-primary" data-toggle="dropdown">
                                <i class="icon fa fa-fw fa-ellipsis-v btn-icon"></i>
                                <i class="icon fa fa-fw fa-caret-down btn-caret"></i>
                            </button>
                            <ul class="Dropdown-menu dropdown-menu dropdown-menu--right">
                                <li class="item-reply">
                                    <button class=" hasIcon" type="btn" title="<?php echo lang('reply');?>">
                                        <i class="icon fa fa-fw fa-reply btn-icon"></i>
                                        <span class="btn-label"><?php echo lang('reply');?>1</span>
                                    </button>
                                </li>
                            </ul>
                        </div>
                    </li>
                    <li class="item-subscription">
                        <div class="Dropdown btn-group SubscriptionMenu">
                    <?php $haya_favorite_check_favorite = haya_favorite_find_by_uid_and_tid($uid, $tid); ?>
                    <?php if (!empty($uid)) { ?>
                        <?php if($haya_favorite_check_favorite){ ?>
                            <!-- 删除收藏 -->
                            <button class="btn SubscriptionMenu-btn hasIcon js-haya-favorite-del" type="btn" data-tid="<?php echo $tid;?>" data-loading-text="正在删除..." title="<?php echo lang('haya_favorite_delete_favorite'); ?>">
                                <i class="icon fa fa-fw fa-star btn-icon" aria-label="<?php echo lang('haya_favorite_delete_favorite'); ?>"></i>
                                <span class="btn-label haya-favorite-btn"><?php echo lang('haya_favorite_delete'); ?></span>
                            </button>
                        <?php } else { ?>
                            <!-- 添加收藏 -->
                            <button class="btn SubscriptionMenu-btn hasIcon js-haya-favorite-add" type="btn" data-tid="<?php echo $tid;?>" data-loading-text="正在收藏..." title="<?php echo lang('haya_favorite_add_favorite'); ?>">
                                <i class="icon fa fa-fw fa-star-o btn-icon" aria-label="<?php echo lang('haya_favorite_add_favorite'); ?>"></i>
                                <span class="btn-label haya-favorite-btn"><?php echo lang('haya_favorite_add'); ?></span>
                            </button>
                        <?php } ?>
                    <?php } else { ?>
                            <button class="btn js-haya-favorite-tip" data-tid="<?php echo $tid;?>" title="<?php echo lang('haya_favorite_add_favorite'); ?>">
                                <i class="icon icon-star-o" aria-label="<?php echo lang('haya_favorite_add_favorite'); ?>"></i>
                                <span class="btn-label haya-favorite-btn"><?php echo lang('haya_favorite_add'); ?></span>
                            </button>
                    <?php } ?>
                            <button class="Dropdown-toggle btn btn--icon js-haya-favorite-show-users <?php if($haya_favorite_check_favorite){ ?>SubscriptionMenu-btn-follow<?php } ?>" data-tid="<?php echo $tid;?>" title="<?php echo lang('haya_favorite_show_favorite_users'); ?>" data-toggle="dropdown">
                                <span class="haya-favorite-user-count"><?php echo intval($thread['favorites']); ?></span>
                            </button>
                            <div class="haya-favoriter dropdown-menu dropdown-menu-right">
                                <div class="text-center d-none haya-favorite-show-users">
                                    <div class="text-left m-0 p-0 col-md-6 mx-auto">
                                        <div class="modal-content">
                                            <div class="modal-header small">
                                                <b><?php echo lang('haya_favorite_users'); ?>（<span class="haya-favorite-user-count"><?php echo intval($thread['favorites']); ?></span>）</b>

                                                <span class="close small p-3 haya-favorite-close js-haya-favorite-show-users" data-dismiss="dodal">X</span>
                                            </div>

                                            <div class="modal-body p-3">
                                                <div class="text-muted haya-favorite-users small break-all">
                                                    <div class="text-muted"><?php echo lang('haya_favorite_loading'); ?></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </li>
                    <li class="item-scrubber">
                        <div class="PostStreamScrubber Dropdown App-titleControl">
                            <button class="btn Dropdown-toggle" data-toggle="dropdown">
                                <span class="Scrubber-index">333</span> of
                                <span class="Scrubber-count">11</span> posts
                                <i class="icon fas fa-sort "></i>
                            </button>
                            <div class="Dropdown-menu dropdown-menu">
                                <div class="Scrubber">
                                    <a class="Scrubber-first win-backtop" href="javascript:">
                                        <i class="icon fa fa-fw fa-angle-double-up "></i> 楼主原帖
                                    </a>
                                    <div class="Scrubber-scrollbar" style="max-height: 166px; cursor: pointer; -webkit-user-select: none;">
                                        <div class="Scrubber-before" style="height: 0%;"></div>
                                        <div class="Scrubber-handle" style="cursor: move; height: 35%;">
                                            <div class="Scrubber-bar"></div>
                                            <div class="Scrubber-info">
                                                <strong><span class="posts"><?php echo $thread['posts'];?></span> 条回复</strong>
                                            </div>
                                        </div>
                                        <div class="Scrubber-after" style="height: 65%;"></div>
                                        <div class="Scrubber-unread" style="top: 0%; height: 100%;">暂不可用</div>
                                    </div>
                                    <a class="Scrubber-last win-backbottom" href="javascript:">
                                        <i class="icon fa fa-fw fa-angle-double-down "></i> <?php echo lang('new_post');?>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </nav>
            <div class="DiscussionPage-stream">
                <div class="PostStream">
                    <!-- 楼主原帖 -->
                    <div class="PostStream-item">
                        <article class="Post CommentPost">
                            <div>
                                <header class="Post-header">
                                    <ul>
                                        <li class="item-user">
                                            <div class="PostUser">
                                                <h3>
                                                    <a href="<?php echo url("user-$thread[uid]");?>" tabindex="-1">
                                                        <img class="Avatar PostUser-avatar lazy" data-original="<?php echo $thread['user_avatar_url'];?>">
                                                        <span class="username"><?php echo $thread['username'];?></span>
                                                    </a>
                                                </h3>
                                                <ul class="PostUser-badges badges">
                                                <?php if($thread['user']['gid']==1) { ?>
                                                    <li>
                                                        <span class="Badge Badge--group--1" title="<?php echo $thread['user']['groupname'];?>" data-toggle="tooltip">
                                                            <i class="icon fa fa-fw fa-wrench Badge-icon"></i>
                                                        </span>
                                                    </li>
                                                <?php } elseif($thread['user']['gid']>1 && $thread['user']['gid']<5) { ?>
                                                    <li>
                                                        <span class="Badge Badge--group--4" title="<?php echo $thread['user']['groupname'];?>" data-toggle="tooltip">
                                                            <i class="icon fa fa-fw fa-user-secret Badge-icon"></i>
                                                        </span>
                                                    </li>
                                                <?php } ?>
                                                </ul>
                                            </div>
                                        </li>
                                        <!-- <li>
                                            <span class="user-level-gw user-1-4-gw user-2-gw">
                                                <i><?php echo $_thread['user']['groupname'];?></i>
                                            </span>
                                        </li> -->
                                        <li class="item-meta">
                                            <div class="PostMeta"><?php echo $thread['create_date_fmt'];?></div>
                                        </li>
                                    </ul>
                                </header>
                                <div class="Post-body message zoomify" isfirst="1">
                                    <?php if($page == 1) { ?>

                                        <!--{hook thread_message_before.htm}-->
                                        <?php echo $first['message_fmt'];?>

                                        <?php echo post_file_list_html($first['filelist']);?>
                                        <!--{hook thread_filelist_after.htm}-->

                                    <?php } else { ?>

                                        <!--{hook thread_message_more_before.htm}-->
                                        <p><a href="<?php echo url("thread-$tid");?>"><?php echo lang('view_thread_message');?></a></p>
                                        <!--{hook thread_message_more_after.htm}-->

                                    <?php } ?>
                                </div>
                                <aside class="Post-actions">
                                    <ul>
                                        <!--{hook thread_update_before.htm}-->
                                        <?php if($allowupdate || $first['allowupdate']) { ?>
                                        <li>
                                            <a class="btn btn--icon btn-flat post_update" href="<?php echo url("post-update-$thread[firstpid]");?>" title="<?php echo lang('edit');?>" data-toggle="tooltip">
                                                <i class="icon fa fa-fw fa-edit btn-icon f-18 icon-edit"></i>
                                            </a>
                                        </li>
                                        <?php } ?>
                                        <?php if($allowdelete || $first['allowdelete']) { ?>
                                        <li>
                                            <a class="btn btn--icon btn-flat post_delete" data-href="<?php echo url("post-delete-$thread[firstpid]");?>" href="javascript:void(0);" isfirst="1" title="<?php echo lang('delete');?>" data-toggle="tooltip">
                                                <i class="icon fa fa-fw fa-trash-o btn-icon f-18 icon-remove"></i>
                                            </a>
                                        </li>
                                        <?php } ?>
                                        <!--{hook thread_delete_after.htm}-->
                                    </ul>
                                </aside>
                            </div>
                        </article>
                    </div>
                <!-- 编辑历史 -->
                <?php if($first['last_update_date']) { ?>
                    <?php $first['last_update_user'] = user_read_cache($first['last_update_uid']);?>
                    <div class="PostStream-item">
                        <article class="Post undefined EventPost DiscussionLockedPost">
                            <i class="icon fa fa-history EventPost-icon" title="编辑历史"></i>
                            <div class="EventPost-info">
                                <?php echo humandate($first['last_update_date']);?>
                            <?php if($gid < 5 && $allowdelete) { ?>
                                <a class="EventPost-user text-primary" href="javascript:void(0);" data-modal-url="<?php echo url("post-update_log_list-".$first['pid']);?>" data-modal-title="编辑历史" data-modal-size="-large">
                                    <span class="username"><?php echo $first['last_update_user']['username'];?></span>
                                </a> 编辑
                            <?php } else { ?>
                                <span class="username"><?php echo $first['last_update_user']['username'];?></span> 编辑
                            <?php } ?>
                                ，原因： <?php echo $first['last_update_reason'];?>。
                            </div>
                        </article>
                    </div>
                <?php } ?>

                    <?php include _include(APP_PATH.'view/htm/post_list.inc.htm'); ?>

                    <?php if(!empty($user)) { ?>
                    <div class="PostStream-item media post newpost media" id="newpost" style="border-bottom: none;">
                        <article class="Post CommentPost">
                            <header class="Post-header">
                                <ul>
                                    <li class="item-user">
                                        <div class="PostUser">
                                            <h3>
                                                <a href="<?php echo url("user-$user[uid]");?>" tabindex="-1">
                                                    <img class="Avatar PostUser-avatar lazy" data-original="<?php echo $user['avatar_url'];?>">
                                                    <span class="username"><?php echo $user['username'];?></span>
                                                </a>
                                            </h3>
                                            <ul class="PostUser-badges badges">
                                                <?php if($thread['user']['gid']==1) { ?>
                                                    <li>
                                                        <span class="Badge Badge--group--1" title="<?php echo $thread['user']['groupname'];?>" data-toggle="tooltip">
                                                            <i class="icon fa fa-fw fa-wrench Badge-icon"></i>
                                                        </span>
                                                    </li>
                                                <?php } elseif($thread['user']['gid']>1 && $thread['user']['gid']<5) { ?>
                                                    <li>
                                                        <span class="Badge Badge--group--4" title="<?php echo $thread['user']['groupname'];?>" data-toggle="tooltip">
                                                            <i class="icon fa fa-fw fa-user-secret Badge-icon"></i>
                                                        </span>
                                                    </li>
                                                <?php } ?>
                                            </ul>
                                        </div>
                                    </li>
                                </ul>
                            </header>
                            <div class="Post-body message">
                                <form action="<?php echo url("post-create-$tid-1");?>" method="post" id="quick_reply_form" class="d-block">
                                    <input type="hidden" name="doctype" value="1" />
                                    <input type="hidden" name="return_html" value="1" />
                                    <input type="hidden" name="quotepid" value="0" />

                                    <textarea class="form-control comments-input" placeholder="文明社会，理性评论。" name="message" id="message"></textarea>
                            </div>
                            <aside class="Post-actions float-left mt-3">
                                <ul>
                                    <li>
                                        <!--{hook thread_quick_reply_left_start.htm}-->
                                        <button type="submit" class="btn btn-primary mr-3" id="submit" data-loading-text="<?php echo lang('submiting');?>..."> <?php echo lang('post_create');?> </button>
                                        <!--{hook thread_quick_reply_left_end.htm}-->
                                    </li>
                                    <li>
                                        <!--{hook thread_quick_reply_right_start.htm}-->
                                        <a class="btn" href="<?php echo url("post-create-$tid");?>" id="advanced_reply">
                                            <i class="fa fa-fw fa-eye-slash btn-icon"></i>
                                            <?php echo lang('advanced_reply');?>
                                        </a>
                                        <!--{hook thread_quick_reply_right_end.htm}-->
                                    </li>
                                </ul>
                            </aside>
                        </article>
                    </div>
                    <?php } else { ?>
                    <div id="flarum-loading">
                        <a class="btn-link" href="<?php echo url('user-login');?>" role="btn" title="<?php echo lang('login');?>">
                            <?php echo lang('login');?>
                        </a>
                        才能发表评论~
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!--{hook thread_end.htm}-->

<?php include _include(APP_PATH.'view/htm/footer.inc.htm');?>

<script>
var jform = $('#quick_reply_form');
var jsubmit = $('#submit');
jform.on('submit', function() {
    jform.reset();
    jsubmit.btn('loading');
    var postdata = jform.serialize();
    $.xpost(jform.attr('action'), postdata, function(code, message) {
        if(code == 0) {
            var s = '<div class="PostStream postlist">'+message+'</div>';
            var jli = $(s).find('.PostStream-item');
            jli.insertBefore($('.PostStream-item').last());
            jsubmit.btn('reset');
            $('#message').val('');

            // 楼层 +1
            var jfloor = $('#newfloor');
            jfloor.html(xn.intval(jfloor.html()) + 1);

            // 回复数 +1
            var jposts = $('.posts');
            jposts.html(xn.intval(jposts.html()) + 1);

        } else if(code < 0) {
            $.alert(message);
            jsubmit.btn('reset');
        } else {
            jform.find('[name="'+code+'"]').alert(message).focus();
            jsubmit.btn('reset');
        }
    });
    return false;
});


// 缩放图片，适应屏幕大小。
function resize_image() {
    var jmessagelist = $('div.message');
    var first_width = jmessagelist.width(); // 815 : 746; //  734 746
    jmessagelist.each(function() {
        var jdiv = $(this);
        var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); //  734 746
        var jmessage_width = Math.min(jdiv.width(), maxwidth);
        jdiv.find('img, embed, iframe, video').each(function() {
            var jimg = $(this);
            var img_width = this.org_width;
            var img_height = this.org_height;
            if(!img_width) {
                var img_width = jimg.attr('width');
                var img_height = jimg.attr('height');
                this.org_width = img_width;
                this.org_height = img_height;
            }
            //var percent = xn.min(100, xn.ceil((img_width / jmessage_width) * 100));
            if(img_width > jmessage_width) {
                if(this.tagName == 'IMG') {
                    jimg.width(jmessage_width);
                    jimg.css('height', 'auto');
                    jimg.css('cursor', 'pointer');
                    jimg.on('click', function() {
                        //window.open(jimg.attr('src'));
                    });
                } else {
                    jimg.width(jmessage_width);
                    var height = (img_height / img_width) * jimg.width();
                    jimg.height(height);
                }
            }
        });
    });
}

// 对于超宽的表格，加上响应式
function resize_table() {
    $('div.message').each(function() {
        var jdiv = $(this);
        jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>');
    });
}

$(function() {
    resize_image();
    resize_table();
    $(window).on('resize', resize_image);
});

// 输入框自动伸缩

var jmessage = $('#message');

$('li[data-active="fid-<?php echo $fid;?>"]').addClass('active');

</script>

<?php if($thread['closed'] && ($gid == 0 || $gid > 5)) { ?>
<script>
jmessage.val('<?php echo lang('thread_has_already_closed');?>').attr('readonly', 'readonly');
</script>
<?php } ?>
<!--{hook thread_js.htm}-->